import { reqC1, reqC2, reqC3 } from '@/api/product/attr/attr'
import type { CategoryObj, CategoryResponseData } from '@/api/product/attr/type'

import { ElMessage } from 'element-plus'
import { defineStore } from 'pinia'
import { ref } from 'vue'

export default defineStore('Category', () => {
  //分类一全部数据
  const c1Arr = ref<CategoryObj[]>()
  //分类1id
  const c1Id = ref<number | string>('')
  //分类一全部数据
  const c2Arr = ref<CategoryObj[]>()
  //分类2id
  const c2Id = ref<number | string>('')
  //分类一全部数据
  const c3Arr = ref<CategoryObj[]>()
  //分类3id
  const c3Id = ref<number | string>('')
  const getC1 = async () => {
    const req: CategoryResponseData = await reqC1()

    if (req.code == 200) {
      c1Arr.value = req.data
    } else {
      ElMessage({
        type: 'error',
        message: '获取分类1错误',
      })
    }
  }
  const getC2 = async () => {
    const req: CategoryResponseData = await reqC2(c1Id.value)

    if (req.code == 200) {
      c2Arr.value = req.data
    } else {
      ElMessage({
        type: 'error',
        message: '获取分类1错误',
      })
    }
  }
  const getC3 = async () => {
    const req: CategoryResponseData = await reqC3(c2Id.value)
    if (req.code == 200) {
      c3Arr.value = req.data
    } else {
      ElMessage({
        type: 'error',
        message: '获取分类1错误',
      })
    }
  }
  return { c1Arr, c2Arr, c3Arr, c2Id, c1Id, c3Id, getC1, getC2, getC3 }
})
